package com.chuyu.cy.controller;

import com.chuyu.cy.contants.PageResult;
import com.chuyu.cy.contants.ResponseCodeEnum;
import com.chuyu.cy.contants.Result;
import com.chuyu.cy.entity.dto.NeedDTO;
import com.chuyu.cy.entity.vo.NeedsVO;
import com.chuyu.cy.service.TemporaryNeedsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author: 周小白
 * @date: 2024-03-15 18:09
 **/
@RestController
@RequestMapping("/needs")
@Tag(name = "需求管理")
public class NeedsController {

    @Resource
    private TemporaryNeedsService temporaryNeedsService;




    /**
     * 获取需求列表
     * @param kindId 需求类型id
     * @param pageNum 页码
     * @return 需求列表
     */
    @GetMapping("/list/{kindId}")
    @Operation(summary = "获取需求列表")
    public Result<PageResult<NeedsVO>> getNeedsList(@PathVariable("kindId") Integer kindId,
                                                    @RequestParam("pageNum") Integer pageNum) {
        Result<PageResult<NeedsVO>> result = new Result<>();

        if (kindId == null) {
            throw new RuntimeException("参数错误");
        }

        PageResult<NeedsVO> pageResult = temporaryNeedsService.getNeedsList(kindId, pageNum, 10);

        if (pageResult == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(pageResult);
    }





    /**
     * 获取需求详情
     * @param id 需求id
     * @return 需求详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "获取需求详情")
    public Result<NeedsVO> getNeedsDetail(@PathVariable("id") Integer id) {
        Result<NeedsVO> result = new Result<>();

        if (id == null) {
            return result.failure(ResponseCodeEnum.NO_PARAM);
        }

        NeedsVO needsVO = temporaryNeedsService.getNeedsDetail(id);

        if (needsVO == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(needsVO);
    }




    /**
     * 发布需求
     * @param needDTO 需求信息
     * @param userId 用户id
     * @return 发布结果
     */
    @PostMapping("/add/{userId}")
    @Operation(summary = "发布需求")
    public Result insertNeeds(@RequestBody NeedDTO needDTO,
                              @PathVariable("userId") Integer userId) {
        Result result = new Result();

        if (needDTO == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        boolean inserted = temporaryNeedsService.insertNeeds(needDTO, userId);

        if (!inserted) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success();
    }





    /**
     * 修改需求信息
     * @param needDTO 需求信息
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改需求信息")
    public Result updateNeeds(@RequestBody NeedDTO needDTO) {
        Result result = new Result();

        if (needDTO == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        Integer updated = temporaryNeedsService.updateNeeds(needDTO);

        if (updated <= 0) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success();
    }




    /**
     * 删除需求信息
     * @param id 需求id
     * @return 删除结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除需求信息")
    public Result deleteNeeds(@PathVariable("id") Integer id) {
        Result result = new Result();

        if (id == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        boolean deleted = temporaryNeedsService.deleteNeed(id);

        if (!deleted) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success();
    }





    /**
     * 获取用户发布的需求列表
     * @param userId 用户id
     * @param pageNum 页码
     * @return 需求列表
     */
    @GetMapping("/published/list/{userId}")
    @Operation(summary = "获取用户发布的需求列表")
    public Result<PageResult<NeedsVO>> getPublishedNeedsList(@PathVariable("userId") Integer userId,
                                                       @RequestParam("pageNum") Integer pageNum) {
        Result<PageResult<NeedsVO>> result = new Result<>();

        if (userId == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        PageResult<NeedsVO> pageResult = temporaryNeedsService.getPublishedNeedsList(userId, pageNum, 10);

        return result.success(pageResult);
    }






    /**
     * 获取用户已匹配的需求列表
     * @param userId 用户id
     * @param pageNum 页码
     * @return 需求列表
     */
    @GetMapping("/matched/list/{userId}")
    @Operation(summary = "获取用户已匹配的需求列表")
    public Result<PageResult<NeedsVO>> getMatchedNeedsList(@PathVariable("userId") Integer userId,
                                                     @RequestParam("pageNum") Integer pageNum) {
        Result<PageResult<NeedsVO>> result = new Result<>();

        if (userId == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        PageResult<NeedsVO> pageResult = temporaryNeedsService.getMatchedNeedsList(userId, pageNum, 10);

        if (pageResult == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(pageResult);
    }






    /**
     * 获取用户未评分的需求列表
     * @param userId 用户id
     * @param pageNum 页码
     * @return 需求列表
     */
    @GetMapping("/unrated/list/{userId}")
    @Operation(summary = "获取用户未评分的需求列表")
    public Result<PageResult<NeedsVO>> getUnratedNeedsList(@PathVariable("userId") Integer userId,
                                                     @RequestParam("pageNum") Integer pageNum) {
        Result<PageResult<NeedsVO>> result = new Result<>();

        if (userId == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        PageResult<NeedsVO> pageResult = temporaryNeedsService.getUnratedNeedsList(userId, pageNum, 10);

        if (pageResult == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(pageResult);
    }




    /**
     * 获取用户已评分的需求列表
     * @param userId 用户id
     * @param pageNum 页码
     * @return 需求列表
     */
    @GetMapping("/rated/list/{userId}")
    @Operation(summary = "获取用户已评分的需求列表")
    public Result<PageResult<NeedsVO>> getRatedNeedsList(@PathVariable("userId") Integer userId,
                                                   @RequestParam("pageNum") Integer pageNum) {
        Result<PageResult<NeedsVO>> result = new Result<>();

        if (userId == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        PageResult<NeedsVO> pageResult = temporaryNeedsService.getRatedNeedsList(userId, pageNum, 10);

        if (pageResult == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(pageResult);
    }





    /**
     * 需求匹配
     * @param needId 需求id
     * @param matcherId 匹配者id
     * @return 匹配结果
     */
    @GetMapping("/match/{needId}")
    @Operation(summary = "需求匹配")
    public Result needMatched(@PathVariable("needId") Integer needId,
                              @RequestParam("matcherId") Integer matcherId) {
        Result result = new Result();

        if (needId == null || matcherId == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        boolean isMatched = temporaryNeedsService.needMatched(needId, matcherId);

        if (!isMatched) {
            return result.failure("匹配失败");
        }

        return result.success();
    }






    /**
     * 发布者评分
     * @param needId 需求id
     * @param score 评分
     * @return 评分结果
     */
    @GetMapping("/rate/publisher/{needId}")
    @Operation(summary = "发布者评分")
    public Result publisherRate(@PathVariable("needId") Integer needId,
                       @RequestParam("score") Integer score) {
        Result result = new Result();

        if (needId == null || score == null) {
            return result.failure(ResponseCodeEnum.NO_PARAM);
        }

        boolean isRated = temporaryNeedsService.publisherRate(needId, score);

        if (!isRated) {
            return result.failure("评分失败");
        }

        return result.success();
    }





    /**
     * 匹配者评分
     * @param needId 需求id
     * @param score 评分
     * @return 评分结果
     */
    @GetMapping("/rate/matcher/{needId}")
    @Operation(summary = "匹配者评分")
    public Result matcherRate(@PathVariable("needId") Integer needId,
                       @RequestParam("score") Integer score) {
        Result result = new Result();

        if (needId == null || score == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        boolean isRated = temporaryNeedsService.matcherRate(needId, score);

        if (!isRated) {
            return result.failure("评分失败");
        }

        return result.success();
    }




    /**
     * 搜索需求
     * @param keyword 关键字
     * @param pageNum 页码
     * @return 需求列表
     */
    @GetMapping("/search")
    @Operation(summary = "搜索需求")
    public Result<PageResult<NeedsVO>> searchNeeds(@RequestParam("keyword") String keyword,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Result<PageResult<NeedsVO>> result = new Result<>();

        if (keyword == null) {
            return result.failure(ResponseCodeEnum.ERROR_PARAM);
        }

        PageResult<NeedsVO> pageResult = temporaryNeedsService.searchNeeds(keyword, pageNum, 10);

        if (pageResult == null) {
            return result.failure(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return result.success(pageResult);
    }

}
